import java.awt.Container;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.util.*;
import java.sql.*;
import java.awt.print.PrinterException;
import java.text.MessageFormat;
import javax.swing.table.DefaultTableModel;



public class ReorderReport extends javax.swing.JFrame {

    /** Creates new form ReorderReport */
    public ReorderReport() {
        initComponents();
        getDatabase();
  }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Reorder Report");

        jLabel1.setText("Items Below Reorder Level");

        jButton1.setText("Back");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Product Number", "Product Name", "Stocks"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton2.setText("Print");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(182, 182, 182)
                .addComponent(jLabel1)
                .addContainerGap(182, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(316, 316, 316)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ReportsProper x = new ReportsProper();
        x.show();
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
         try {
          MessageFormat headerFormat = new MessageFormat("Reorder Report");
          MessageFormat footerFormat = new MessageFormat("- {0} -");
          jTable1.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
        } catch (PrinterException pe) {
          System.err.println("Error printing: " + pe.getMessage());
        }
    }//GEN-LAST:event_jButton2ActionPerformed



    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ReorderReport().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

   DB DB = new DB();
   Container c = new Container();
   int rowCount = 0;
   int type = 0;
   Connection conn;
   PreparedStatement pstmt;
   Statement stmt;
   ResultSet rset;
   String query;

  public void setValues(Vector row, int rowCount){
            Object z[] = {null,null,null};
            DefaultTableModel model = (javax.swing.table.DefaultTableModel)jTable1.getModel();
            jTable1.setModel(model);

            if(rowCount > 15){
                int q = rowCount - 15;
                for(int w = 0; w < q; w++){
                    model.addRow(z);
                }
            }

       	int columnNum = 0;
       	for(int i = 1; i <= rowCount; i++){
		for(columnNum = 0; columnNum < 3; columnNum++){
       	jTable1.setValueAt(row.elementAt(columnNum+(3*(i-1))), i-1, columnNum);}
       	columnNum = 0;}
   }

    public void getDatabase(){

            Vector row = new Vector();
            rowCount = 0;

            try {
                       DB.connect();
                       conn = DB.getConn();

                       query = "select product.productNo, product.name, stocks.quantity from product, stocks where product.reorderlevel >= stocks.quantity AND product.productNo = stocks.productNo";
                       pstmt = conn.prepareStatement(query);
                       rset = pstmt.executeQuery();


                       while(rset.next())
                       {
                             row.addElement(rset.getString("productNo"));
                             row.addElement(rset.getString("name"));
                             row.addElement(rset.getString("quantity"));
                             
                             rowCount++;
                       }
                             setValues(row, rowCount);
                    DB.disconnect();
                    pstmt.close();

             } catch (Exception ex) {
                   JOptionPane.showMessageDialog(null, "Error: "+ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
             }

       }
}